package org.dromara.shopping.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.shopping.domain.MaterialGroup;
import org.dromara.shopping.service.IMaterialGroupService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 素材分组
 *
 * @author Mr.Zhou
 * @date 2024-10-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/shopping/material/group")
public class MaterialGroupController extends BaseController {

    private final IMaterialGroupService materialGroupService;

    /**
     * 查询素材分组列表
     */
//    @SaCheckPermission("shopping:materialGroup:list")
    @GetMapping("/list")
    public TableDataInfo<MaterialGroup> list(MaterialGroup materialGroup, PageQuery pageQuery) {
        QueryWrapper<MaterialGroup> qw = new QueryWrapper<>();
        LoginUser loginUser = LoginHelper.getLoginUser();
        if(loginUser.getRoleKey().equals("organ")){
            qw.eq("organ_id", loginUser.getOrganId());
        }
        qw.orderByDesc("id");
        TableDataInfo<MaterialGroup> td = PageUtils.buildDataInfo(materialGroupService.page(PageUtils.buildPage(),qw));
        List<MaterialGroup> list = td.getRows();
        return td;
    }

    /**
     * 获取素材分组详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("shopping:materialGroup:query")
    @GetMapping("/{id}")
    public R<MaterialGroup> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer id) {
        return R.ok(materialGroupService.getById(id));
    }

    /**
     * 新增素材分组
     */
//    @SaCheckPermission("shopping:materialGroup:add")
    @Log(title = "素材分组", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MaterialGroup materialGroup) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if(loginUser.getRoleKey().equals("organ")){
            materialGroup.setOrganId(loginUser.getOrganId());
        }
        return toAjax(materialGroupService.save(materialGroup));
    }

    /**
     * 修改素材分组
     */
//    @SaCheckPermission("shopping:materialGroup:edit")
    @Log(title = "素材分组", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MaterialGroup materialGroup) {
        return toAjax(materialGroupService.updateById(materialGroup));
    }

    /**
     * 删除素材分组
     *
     * @param ids 主键串
     */
    @SaCheckPermission("shopping:materialGroup:remove")
    @Log(title = "素材分组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Integer[] ids) {
        return toAjax(materialGroupService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
